import React from 'react';
import { Spin } from 'antd';
import { WeaErrorPage, WeaTools,WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
const { ls } = WeaTools;
import { isEqual, isEmpty } from 'lodash';
import EHeader from '../common/EHeader';
import { NoRightCom } from '../../util/common';
import AudioCom from './Audio';
import FooterMore from "../common/FooterMore";
//元素组件
class Audio extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            edata: null,
            refresh: false,
        }
        this.handleEData = this.handleEData.bind(this);
        this.handleRefresh = this.handleRefresh.bind(this);
        this.handleDelete = this.handleDelete.bind(this);
    }
    handleRefresh(){
        this.setState({
            refresh: true,
        });
        this.handleEData(this.props.config.params);
    }
    handleEData(params){
        const { eid, api } = params;
        WeaTools.callApi(api || '/api/portal/element/audio', 'POST', {...params}).then(result=> {
            ls.set('edata-'+eid, result);
            this.setState({
                edata: result,
                refresh: false,
            });
       });
    }
    handleDelete(){
        this.setState({
            edata: null,
        });
        ls.set('edata-'+this.props.config.params.eid, null);
    }
    componentDidMount(){
        const { config = {} } = this.props;
        const { params = {} } = config;
        const { eid } = params;
        const edata = ls.getJSONObj(`edata-${eid}`);
        if (config.islsdata && edata) {
            this.setState({ edata });
        } else {
            this.handleEData(params);
        }
    }
    componentWillReceiveProps(nextProps){
        const { item, params, isHasRight } = this.props.config;
        const config = { item, params, isHasRight };
        const _config = { 
            item: nextProps.config.item, 
            params: nextProps.config.params, 
            isHasRight: nextProps.config.isHasRight,
        };
        if(!isEqual(config, _config) || nextProps.config.isRender){
            this.handleEData(nextProps.config.params);
        }
    }
    shouldComponentUpdate(nextProps, nextState){
        const { item, params, isHasRight } = this.props.config;
        const config = { item, params, isHasRight };
        const _config = { 
            item: nextProps.config.item, 
            params: nextProps.config.params, 
            isHasRight: nextProps.config.isHasRight,
        };
        return !isEqual(this.state, nextState) || !isEqual(config, _config)
    }
    render() {
        const { config } = this.props;
        const { edata, refresh } = this.state;
        if(!config || !edata) return <div></div>
        const { isHasRight, item } = config;
        const { eid, ebaseid, content, header, contentview, style } = item;
        const { style: style1= {} } = contentview;
        let EContentHtml = <div/>;
        if(isHasRight){
            if(!isEmpty(edata)) EContentHtml = <AudioCom ecId={`${this && this.props && this.props.ecId || ''}_AudioCom@gm6hex`} data={edata.data} eid={eid} esetting={edata.esetting}/>;
        }else{
            EContentHtml = <NoRightCom ecId={`${this && this.props && this.props.ecId || ''}_NoRightCom@qg8al3`}/>
        }
        return <div className="item"
                style={style}
                id={`item_${eid}`} 
                data-eid={eid} 
                data-ebaseid={ebaseid} 
                data-needRefresh={item.needRefresh} 
                data-cornerTop={item.cornerTop} 
                data-cornerTopRadian={item.cornerTopRadian} 
                data-cornerBottom={item.cornerBottom} 
                data-cornerBottomRadian = {item.cornerBottomRadian}>
                <EHeader ecId={`${this && this.props && this.props.ecId || ''}_EHeader@jz754l`} config={config} handleDelete={this.handleDelete} handleRefresh={this.handleRefresh}/>
                <div className="setting" id={`setting_${eid}`}></div>
                <div className="content" id={`content_${eid}`} style={{width:'auto',_width:'100%'}}>
                    <div className="content_view" id={`content_view_id_${eid}`} style={{height: style1.height}}>
                        <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@8jku32`} spinning={refresh}>{EContentHtml}</Spin>
                    </div>
                    <div style={{textAlign: 'right'}} id={`footer_${eid}`}>
                        <FooterMore ecId={`${this && this.props && this.props.ecId || ''}_FooterMore@vm3gna`} config={this.props.config}/>
                    </div>
                </div>
            </div>;
    }
}


class MyErrorHandler extends React.Component {
    render() {
        const hasErrorMsg = this.props.error && this.props.error !== "";
            return ( <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@8o0kt6`} msg = { hasErrorMsg ? this.props.error :getLabel(385218,"对不起，该页面异常，请联系管理员！")
                }/>
        );
    }
}
Audio = WeaTools.tryCatch(React, MyErrorHandler, { error: "" })(Audio);
export default Audio;



